package com.asd.map.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.asd.map.entity.*;
import com.asd.map.mapper.*;
import com.asd.map.service.ApplyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ApplyServiceImpl extends ServiceImpl<MapApplyMappper, Map_apply> implements ApplyService {

    @Autowired
    private MapApplyMappper mapApplyMappper;

    @Autowired
    private Line_styleMapper line_styleMapper;

    @Autowired
    private Surface_styleMapper surface_styleMapper;

    @Autowired
    private Point_styleMapper point_styleMapper;

    @Autowired
    private Tagging_styleMapper tagging_styleMapper;


    @Override
    public List<Map_apply> selectApplyPage(Page<Map_apply> page) {
        QueryWrapper<Map_apply> queryWrapper = new QueryWrapper<>();
        //  判断年龄大于20岁的，这里用的是mybatis-plus的条件构造器
        //        queryWrapper.gt("age",20);
        /**
         *    加上false就不查询总记录数啦,如果不需要的话可以加上false，这样就会少一条sql语句
         *   Page<Student> page = new Page<>(current,size,false);
         */
        //        Page<Information> page1 = new Page<>();
        //   queryWrapper  可以为null  不进行条件判断
        //  返回list 调用  selectPage
        IPage<Map_apply> applyIPage = mapApplyMappper.selectPage(page, queryWrapper);


        //getRecords()分页对象记录列表
        List<Map_apply> records = applyIPage.getRecords();
        return records;
    }

    @Override
    public void updetViewById(String id, String view) {
        mapApplyMappper.updetViewById(id, view);
    }

    @Override
    public void updateById1(String str, String id) {
        mapApplyMappper.updateById1(str, id);
    }

    @Override
    public void deleteStyleById(String id) {

        //根据应用id删除线样式
        List<Line_style> line_stylesList = line_styleMapper.getByApplyId(id);
        for (int i = 0; i < line_stylesList.size(); i++) {
            line_styleMapper.deleteByApplyId(id);
        }
        //根据应用id删除面样式
        List<Surface_style> surface_styleList = surface_styleMapper.getByApplyId(id);
        for (int i = 0; i < surface_styleList.size(); i++) {
            surface_styleMapper.deleteByApplyId(id);
        }
        //根据应用id删除点样式
        List<Point_style> point_styleList = point_styleMapper.getByApplyId(id);
        for (int i = 0; i < point_styleList.size(); i++) {
            point_styleMapper.deleteByApplyId(id);
        }
        //删除标注样式
        Map_apply mapApply = mapApplyMappper.selectById(id);
        String data = mapApply.getData();

        if (StringUtils.isNotEmpty(data)) {

            //将字符串data转为list
            List<Map<String, Object>> listData = (List<Map<String, Object>>) JSONArray.parse(data);
            for (int i = 0; i < listData.size(); i++) {
                Map<String, Object> map = listData.get(i);
                String tid = map.get("tid").toString();
                tagging_styleMapper.deleteByTid(tid);
            }
        }
    }

    @Override
    public void changeBaseLayer(String id, String baseLayer) {
        mapApplyMappper.changeBaseLayer(id, baseLayer);
    }
}
